const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

const devMode = process.env.NODE_ENV !== 'production'

module.exports = {
  mode: devMode ? 'development' : 'production',
  devtool: devMode ? "inline-source-map" : 'hidden-source-map',
  entry: "./src/components/index.ts",
  output: {
    path: __dirname + '/dist',
    filename: "bricks.js"
  },
  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        sourceMap: true // set to true if you want JS source maps
      }),
      new OptimizeCSSAssetsPlugin({})
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // both options are optional
      filename: "bricks.css",
      chunkFilename: "[id].css"
    })
  ],
  resolve: {
    // Add `.ts` and `.tsx` as a resolvable extension.
    extensions: [".ts", ".tsx", ".js"]
  },
  module: {
    rules: [
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
      { test: /\.tsx?$/, use: "ts-loader" },
      {
        test: /\.scss$/, use: [
          devMode ? 'style-loader' : MiniCssExtractPlugin.loader, // creates style nodes from JS strings or extra css
          "css-loader", // translates CSS into CommonJS
          "sass-loader", // compiles Sass to CSS
        ]
      },
      {
        test: /(\.(eot|ttf|woff|woff2)|font\.svg)$/,
        loader : 'file-loader',
        options: { outputPath: 'fonts/' }
      },
    ]
  }
};
